package io.writeFile;

import java.io.*;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-28 15:11
 * @version: 1.0
 */
public class WriteInFile {
    public static void main(String[] args) {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(isr);

        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream("./other/src/io/writeFile/old.txt", true);
            bos = new BufferedOutputStream(fos);
            while (true) {
                String s = bufferedReader.readLine();
                if (s.equals("e")) break;
                bos.write(s.getBytes());
                bos.write('\n');
//                flush清空缓存流，会写入文件
//                bos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (bos != null) {
                    //关闭字节缓冲输出流，会自动写入文件
                    bos.close();
                }
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
